package com.sgb.kjwl.viewmodel.machine;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.LeaseOrderInfoEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.lease.LeaseOrderReceiveEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseOrderEvent;
import com.sgb.kjwl.view.event.WebEvent;
import com.sgb.kjwl.view.ui.activity.self.lease.LeaseOrderReceiveActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseOrderReceiveModel extends BaseViewModel {
    public ObservableField<LeaseOrderReceiveEntity> addEntity = new ObservableField<>(new LeaseOrderReceiveEntity());
    private LeaseOrderReceiveActivity activity;

    public LeaseOrderReceiveModel(LeaseOrderReceiveActivity activity) {
        this.activity = activity;
    }

    //接单
    public void rentInOrderReceive() {
        APIManager.getInstance().getLeaseAPI().acceptOrder(addEntity.get()).enqueue(new ICallBack<BaseEntity>(true) {
            @Override
            public void onSuccess(BaseEntity data) {
                MToast.showToast(activity, "提交成功");
                //发消息到H5刷新数据
                EventBus.getDefault().postSticky(WebEvent.make().setMessageTag(EventConstant.EVENT_WEBVIEW_RELOAD));
                EventBus.getDefault().postSticky(LeaseOrderEvent.make());
                activity.finish();
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(activity, "服务器链接失败");
            }
        });
    }


    //获取申请部门
    public void getDepartment() {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getUserDepartmentImg().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    activity.showDepartment(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //租赁订单-订单详情
    public void getOrderDetail(String orderNo) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getOrderDetail(orderNo).enqueue(new Callback<BaseEntity<LeaseOrderInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LeaseOrderInfoEntity>> call, Response<BaseEntity<LeaseOrderInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    activity.showOrderDetail(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LeaseOrderInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 出租快照详情
     *
     * @param type
     * @param id
     */
    public void getH5UrlByOrder(String type, String id) {
        String name = "";
        if ("1".equals(type)) {
            name = "mechanicsDetail";//机械
        } else if ("2".equals(type)) {
            name = "equipmentDetails";//设备
        } else if ("3".equals(type)) {
            name = "vehicleDetails";//车辆
        } else if ("4".equals(type)) {
            name = "propertyDetails";//房产
        } else if ("5".equals(type)) {
            name = "landDetails";//土地
        } else if ("6".equals(type)) {
            name = "factoryDetails";//工厂
        } else if ("7".equals(type)) {
            name = "farmDetail";//农机
        }
        if (TextUtils.isEmpty(name)) {
            return;
        }
        DialogHelper.showProgressDialog(activity,  "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + id;
                    WebViewX5HostActivity.start(activity,url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
